#!/usr/bin/ksh

#########################################################################
#									#
# Name:		mkpkg.aixp (Shell script)				#
#									#
# Completed:	26th March, 2001.					#
#									#
# Updated:	9th November, 2012.					#
#									#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
#									#
# Arguments:	-V		Prints current codemgr packager 	#
#				compatibility - currently 1.		#
#									#
# 		-P -r rel [-x cfg] [-v] [-f]				#
#		-P		Preview mode - do not actually create	#
#				the package in question.		#
#		-r		Indicate the number of the release to	#
#				create - assigned by the 		#
#				mkpkg wrapper script usually.		#
#		-x		Specify an alternative cfg file - 	#
#				the default will be "cfg".		#
#		-f		Force create - if a version of this	#
#				release already exists it should be	#
#				over-written.				#
#									#
# Purpose:	This creates a LPP package based on the files in	#
#		the specified source directories. The toplevel mkpkg	#
#		will have checked that all files are checked in, and	#
#		give the relevant version information for the 		#
#		release.						#
#									#
#		The lppbuild script must be available in the current	#
#		path for this functionality to work.			#
#									#
#		lppbuild is free software and is available from the	#
#		UCLA ftp site.						#
#									#
# Limitations:	1 - No use of the more complex features of LPP		#
#		    packages are yet available - since this would limit	#
#		    compatibility with other package types.		#
#									#
# Environment:	RELMGR_CFG	- Defines the file name containing the	#
#				  configuration settings for the 	#
#				  release manager for this user.	#
#									#
# Exit Codes:	0  - Package created successfully 			#
#		>0 - An error has occurred / see standand error.	#
# 									#
#########################################################################

[[ "$1" = DESC ]] && {
	print "AIX LPP package";
	exit 0;
}

function logmsg
{
	[[ $ARG_verbose -eq 0 ]] && return
	print "MSG  : $1"
}

function logwarn
{
	print "WARN : $1"
}

function logwarnfile
{
	while read cline
	do
		print "WARN : $cline"
	done < $1
}

function logerr
{
	print -u2 "ERROR: $1"
	[[ -n "$2" ]] && exit $2
}

function logerrfile
{
	while read cline
	do
		print "ERROR: $cline"
	done < $1
	[[ -n "$2" ]] && exit $2
}

SCRIPT=$(basename $0)
function loadvar 
{
	#################################################################
	# If we do not currently have a pkg directory, then create one!	#
	#################################################################

	[[ ! -d $1/pkg ]] && {
		mkdir -p $1/pkg
		logwarn "pkg directory auto-created!";
	}

	[[ ! -f $1/pkg/$CFGFILE ]] && {
		logerr "Configuration file \"$1/pkg/$CFGFILE\" does not exist." 5;
	}
	awk -v searchfor="$2" '$1 == "#" {next} $1 ~ searchfor {split($0,a,"="); if(substr(a[1],1,length(searchfor)) == searchfor) { printf("%s %s\n",a[1],a[2]);}}' \
		$1/pkg/$CFGFILE
	return 0
}

function usage
{
	print -u2 "Usage: $SCRIPT -r release [-f] [-v]

Written by Simon Edwards 2000 - 2003, proprius.co.uk.
Please see the codemgr_$SCRIPT(1) manual page for more details.
"
exit 1
}

[[ -z "$RELMGR_CFG" ]] && {
	logerr "Variable \"RELMGR_CFG\" is not defined." 10;
}

[[ ! -f "$RELMGR_CFG" ]] && {
	logerr "\"RELMGR_CFG\" setting ($RELMGR_CFG) is not valid." 11;
}

LPATH=$(dirname $0)
LPP_TOOLS=$LPATH/../utils
[[ ! -d "$LPP_TOOLS" ]] && {
	logerr "The $LPP_TOOLS directory could not be found." 12;
}

[[ ! -f $LPP_TOOLS/lppbuild ]] && {
	logerr "Unable to find the the lppbuild utility in $LPP_TOOLS." 13;
}

#########################################################################
# Now source the configuration information...				#
#########################################################################

. $RELMGR_CFG

export PROJECT_PREFIX PROJECT_PKGDIR PROJECT_RELDB VCTYPE
export DEFAULT_GROUP DEFAULT_PERMS DEFAULT_OWNER IGNORE_SUFFIXES
export FINAL_DEPOT BUILD_DEPOT

#########################################################################
# Now we have the basic details we need to parse the command line	#
# arguments to ensure everything looks ok.				#
#########################################################################

VERSION=0
FORCE=0
RELEASE=""
ARG_verbose=0
CFGFILE=""
integer PREVIEW=0

while getopts ":VPfvr:x:" argval
do
	case $argval in
		V)	VERSION=1		;;
		P)	PREVIEW=1		;;
		f)	FORCE=1			;;
		v)	ARG_verbose=1		;;
		x)	CFGFILE=$OPTARG		;;
		r)	RELEASE=$OPTARG		;;
		*)	usage
	esac
done

if [[ $VERSION = 1 ]]
then
	print "1"
	exit 0
fi

#########################################################################
# If the wrapper has been to generate the package it will have set the	#
# BUILD_IN_TMP variable, which now becomes the PROJECT_PREFIX for this	#
# particular package build.						#
#########################################################################

print "BUILD_IN_TMP=$BUILD_IN_TMP"
if [[ -n "$BUILD_IN_TMP" && -d $BUILD_IN_TMP ]]
then
	logmsg "PROJECT_PREFIX modified to $BUILD_IN_TMP"
	PROJECT_PREFIX=$BUILD_IN_TMP
fi

PATH=$PATH:$LPP_TOOLS
lppbuild="$(which lppbuild 2>/dev/null)"

[[ $(print "$lppbuild" | wc -w) -ne 1 ]] &&
	logerr "Unable to find copy of \"lppbuild\" utility" 22

[[ -z "$CFGFILE" ]] && CFGFILE=cfg
[[ -z "$RELEASE" ]] && usage

#########################################################################
# We now work out the current product name, using the current directory	#
# path as a starting point. If the current directory path is not	#
# path of the PROJECT_PREFIX then we need to raise an error.		#
#########################################################################

project_name=$(print $PWD|
	awk -v projs=$PROJECT_PREFIX '{if(substr($1,1,length(projs))!=projs) exit(0);
		split(substr($1,2+length(projs)),a,"/");
		print a[1];}')

if [[ -z "$project_name" ]]
then
	logerr "Current path is invalid for a project!"
	logerr "Path must be sub-directory of \"$PROJECT_PREFIX\"." 12
fi

logmsg "Project name ascertained to be: $project_name"

#########################################################################
# We now work out the current product name, using the current directory	#
# path as a starting point. If the current directory path is not	#
# path of the PROJECT_PREFIX then we need to raise an error.		#
#########################################################################

project_name=$(print $PWD|
	awk -v projs=$PROJECT_PREFIX '{if(substr($1,1,length(projs))!=projs) exit(0);
		split(substr($1,2+length(projs)),a,"\/");
		print a[1];}')

if [[ -z "$project_name" ]]
then
	logerr "Current path is invalid for a project!"
	logerr "Path must be sub-directory of \"$PROJECT_PREFIX\"." 12
fi

pkg_name=$(loadvar $PROJECT_PREFIX/$project_name NAME|awk '{print $2}')

if [[ -z "$pkg_name" ]]
then
	pkg_name=$project_name
	logmsg "Package name defaulted to: $pkg_name"
else
	logmsg "Package name is: $pkg_name"
fi

if [[ -n "$FINAL_DEPOT" ]]
then
	cver="$(print $RELEASE | sed 's/\./-/g')"
	pkgname="$project_name.bff"
	[[ -f $FINAL_DEPOT/$pkgname ]] && {
		[[ $FORCE -eq 1 ]] && logwarn "Existing package $pkgname will be overwritten." || logerr "Package $pkgname already exists" 21;
	}
fi

logmsg "Project source tree named as: $project_name"

#########################################################################
# Before we do anything make sure we are able to load the minimum	#
# number of details to define the project package.			#
#########################################################################

pkg_depot=$(loadvar $PROJECT_PREFIX/$project_name DEPOT.AIXP|awk '{print $2}')
[[ -z "$pkg_depot" ]] &&
	pkg_depot=$(loadvar $PROJECT_PREFIX/$project_name DEPOT|awk '{print $2}')

pkg_desc=$(loadvar $PROJECT_PREFIX/$project_name DESCRIPTION|awk '{for(n=2; n<=NF; n++) printf("%s ",$n);}')
pkg_license=$(loadvar $PROJECT_PREFIX/$project_name LICENSE s|awk '{for(n=2; n<=NF; n++) printf("%s ",$n);}')
pkg_ignore_suffixes=$(loadvar $PROJECT_PREFIX/$project_name IGNORE_SUFFIXES s|awk '{print $2}' | sed 's/"//g')

if [[ -z "$pkg_ignore_suffixes" && -n "$IGNORE_SUFFIXES" ]]
then
	pkg_ignore_suffixes="$IGNORE_SUFFIXES"
	logmsg "Ignored suffixes for packaging: $pkg_ignore_suffixes"
fi

[[ -z "$pkg_depot" ]] && {
	[[ -n "$BUILD_DEPOT" ]] && pkg_depot="$BUILD_DEPOT" ||
		pkg_depot="$HOME/aixp_depot";
	logwarn "Defaulting package build depot to $pkg_depot";
}

[[ -z "$pkg_desc" ]] &&
	pkg_desc="Default short description for $pkg_name" &&
	logwarn "Defaulting short package description"

[[ -z "$pkg_ldesc" ]] &&
	pkg_ldesc="Default long description for $pkg_name" &&
	logwarn "Defaulting long package description"

[[ -z "$pkg_license" ]] &&
	pkg_license="GPL" &&
	logwarn "Defaulting license to $pkg_license"

#########################################################################
# Create the depot directory if it does not yet exist.			#
#########################################################################

[[ ! -d "$pkg_depot" ]] && {
	mkdir -p $pkg_depot 2>/dev/null || {
		logerr "Unable to create package depot $pkg_depot" 14;
	} ; 
	logmsg "Created Package depot: $pkg_depot";
}

#########################################################################
# Optional single word configuration variables.				#
#########################################################################

logmsg "Loading optional configuration parameters..."

pkg_cat=$(loadvar $PROJECT_PREFIX/$project_name CATEGORY|awk '{print $2}')
pkg_osrelease=$(loadvar $PROJECT_PREFIX/$project_name OSRELEASE|awk '{print $2}')
pkg_topdir=$(loadvar $PROJECT_PREFIX/$project_name TOPDIR|awk '{print $2}')
pkg_preinstall=$(loadvar $PROJECT_PREFIX/$project_name PREINSTALL|awk '{print $2}')
pkg_postinstall=$(loadvar $PROJECT_PREFIX/$project_name POSTINSTALL|awk '{print $2}')
pkg_checkinstall=$(loadvar $PROJECT_PREFIX/$project_name CHECKINSTALL|awk '{print $2}')
pkg_preremove=$(loadvar $PROJECT_PREFIX/$project_name PREREMOVE|awk '{print $2}')
pkg_postremove=$(loadvar $PROJECT_PREFIX/$project_name POSTREMOVE|awk '{print $2}')
pkg_dependencies=$(loadvar $PROJECT_PREFIX/$project_name DEPENDS_ON|awk '{print $2}')
pkg_vendor1=$(loadvar $PROJECT_PREFIX/$project_name VENDOR1|awk '{print $2}')
pkg_vendor2=$(loadvar $PROJECT_PREFIX/$project_name VENDOR2|awk '{print $2}')
pkg_vendor3=$(loadvar $PROJECT_PREFIX/$project_name VENDOR3|awk '{print $2}')

[[ -z "$pkg_vendor1" ]] && 
	pkg_vendor1="NA" &&
	logmsg "Vendor tag defaulted to: $pkg_vendor1"

[[ -z "$pkg_vendor2" ]] && 
	pkg_vendor2="NA" &&
	logmsg "Vendor title defaulted to: $pkg_vendor2"

[[ -z "$pkg_vendor3" ]] && 
	pkg_vendor3="NA" &&
	logmsg "Vendor description defaulted to: $pkg_vendor3"

[[ -z "$pkg_topdir" ]] &&
	pkg_topdir="/" &&
	logmsg "Package top directory defaulted to: $pkg_topdir"

[[ "$pkg_topdir" != "/" ]] &&
	logerr "LPP package types must have a TOPDIR setting of /" 22

[[ -z "$pkg_cat" ]] &&
	pkg_cat="general" &&
	logmsg "Package category defaulted to: $pkg_cat"

[[ -z "$pkg_osrelease" ]] &&
	pkg_osrelease="?.1[01].*" &&
	logmsg "Package OS release defaulted to: $pkg_osrelease"

#########################################################################
# Now we have dumped the SCCS release information for this release	#
# we need to generate the .psf file to package the software...		#
#########################################################################

trap "cd /tmp; logmsg \"Remove temporary directory $pkg_depot/$pkg_name\"; chmod -R 777 $pkg_depot/$pkg_name; rm -rf $pkg_depot/$pkg_name" EXIT

mkdir -p $pkg_depot/$pkg_name/allfiles 2>/dev/null || {
	logerr "Unable to create build directory - $pkg_depot/$pkg_name/allfiles" 14;
}
print "VERSION=$RELEASE" >$pkg_depot/$pkg_name/allfiles/control
print "DESCRIPTION=\"$pkg_desc\"" >>$pkg_depot/$pkg_name/allfiles/control

mkdir $pkg_depot/$pkg_name/allfiles/root 2>/dev/null || {
	logerr "Unable to create build root directory - $pkg_depot/$pkg_name/allfiles/root" 14;
}

#########################################################################
# Generate the copyright file...					#
#########################################################################

copyright=$pkg_depot/$pkg_name/copyright

if [[ $(print $pkg_license|wc -w) -eq 1 ]]
then
	pkg_license="This package is made available under the following license agreement:\n$pkg_license"
fi

print "$pkg_license" > $copyright 2>/dev/null || {
	logerr "Unable to write license file - $copyright" 15;
}

#########################################################################
# Include details of a post installation script if specified.		#
# We use if as the post_i script for the package, which is run at the	#
# end of the package installation.					#
#########################################################################

postinstall_script=$pkg_depot/$pkg_name/allfiles/post_i
if [[ -n "$pkg_postinstall" ]]
then
	if [[ ! -f "$pkg_postinstall" ]]
	then
		logwarn "Unable to find post install script: $pkg_postinstall"
	else
		cp $pkg_postinstall $postinstall_script 2>/dev/null ||
			logerr "Unable to write post install script - $postinstall_script" 15
		logmsg "Post installation script copied successfully."
	fi
fi

#########################################################################
# Include details of a post remove script if specified.			#
# We use if as the unpost_i script for the package, which is run at the	#
# end of the package installation.					#
#########################################################################

postremove_script=$pkg_depot/$pkg_name/allfiles/unpost_i
if [[ -n "$pkg_postinstall" ]]
then
	if [[ ! -f "$pkg_postremove" ]]
	then
		logwarn "Unable to find post remove script: $pkg_postremove"
	else
		cp $pkg_postremove $postremove_script 2>/dev/null ||
			logerr "Unable to write post remove script - $postremove_script" 15
		logmsg "Post removal script copied successfully."
	fi
fi

#########################################################################
# Include details of a pre removal script if specified.			#
# We use if as the pre_rm script for the package, which is run prior to	#
# deleting the package.							#
#########################################################################

preremove_script=$pkg_depot/$pkg_name/allfiles/pre_rm
if [[ -n "$pkg_preremove" ]]
then
	if [[ ! -f "$pkg_preremove" ]]
	then
		logwarn "Unable to find pre remove script: $pkg_preremove"
	else
		cp $pkg_preremove $preremove_script 2>/dev/null ||
			logerr "Unable to write pre remove script - $preremove_script" 15
		logmsg "Pre remove script copied successfully."
	fi
fi

#########################################################################
# Include details of a pre installation script if specified.		#
# We use if as the pre_i script for the package, which is run prior to	#
# installation of the package.						#
#########################################################################

preinstall_script=$pkg_depot/$pkg_name/allfiles/pre_d
if [[ -n "$pkg_preinstall" ]]
then
	if [[ ! -f "$pkg_preinstall" ]]
	then
		logwarn "Unable to find pre install script: $pkg_preinstall"
	else
		cp $pkg_preinstall $preinstall_script 2>/dev/null ||
			logerr "Unable to write pre install script - $preinstall_script" 15
		logmsg "Pre install script copied successfully."
	fi
fi

#########################################################################
# We need to show the details of any prerequisites if they have been	#
# included in the configuration file.					#
#########################################################################

requisites=$pkg_depot/$pkg_name/allfiles/requisites
if [[ -n "$pkg_dependencies" ]]
then
	for depend_on in $(print $pkg_dependencies|tr ',' ' ')
	do
		d_pkg=$(print $depend_on|awk -v FS=: '{print $1}')
		d_minver=$(print $depend_on|awk -v FS=: '{print $2}')
		print "*prereq $d_pkg $d_minver" >>$requisites 2>/dev/null ||
			logerr "Unable to write to requisites file - $requisites" 16
	done
fi

#########################################################################
# Now we copy all the files that make up the package under the		#
# root directory for this package, setting the correct permissions	#
# as we go - leaving the directories until list.			#
#########################################################################

myid=$(whoami)
[[ "$myid" != root ]] && logwarn "Not root user - can not set owner & group on files!"

setpermlist=""
LPP_ROOTDIR="$pkg_depot/$pkg_name/allfiles/root"
logmsg "Building package contents..."
predir=""
predestdir=""
trap "" EXIT
loadvar $PROJECT_PREFIX/$project_name DIR: |
while read dir dest
do
	integer ctfiles=0
        dir=$(print $dir|awk -v FS=: '{print $2}')
	[[ -f  $PROJECT_PREFIX/$pkg_name/$dir/.ignore ]] && {
		logmsg "Ignoring directory $dir";
		continue;
	}
	mydir=$(print "$dest" | awk -v FS=, '{print $1}')
	mkdir -p $LPP_ROOTDIR/$mydir 2>/dev/null || {
		logerr "Could not create directory: $LPP_ROOTDIR/$mydir" 17;
	}
	logmsg "Copying files to $mydir ..."

	#################################################################
	# Get the user, group and permissions, if specified		#
	#################################################################

	user=$(print $dest|awk -v FS=, '{print $2}')
	user=${user:-$DEFAULT_OWNER}
	user=${user:-root}
	group=$(print $dest|awk -v FS=, '{print $3}')
	group=${group:-$DEFAULT_GROUP}
	group=${group:-root}
	perms=$(print $dest|awk -v FS=, '{print $4}')
	perms=${perms:-$DEFAULT_PERMS}
	perms=${perms:-750}
	destdir=$(print $dest|awk -v FS=, '{print $1}')

	setpermlist="$LPP_ROOTDIR/$mydir|$user|$group|$perms $setpermlist"

	#################################################################
	# Now check if extended DIR:ddd(file)... syntax has been	#
	# used...							#
	#################################################################

	pickfiles=$(print $dir|
		awk '{s1=index($0,"("); s2=index($0,")");
			if(s1>0 && s2>0) {
				print substr($0,s1+1,s2-s1-1);
			}
		     }')

	[[ -z "$pickfiles" ]] && pattern="*"
	[[ -n "$pickfiles" ]] && pattern=$(print "$pickfiles" | tr ',' ' ')

	mycd=$PWD

	#################################################################
	# Chop of the filtered files bit from the directory name	#
	# if required.							#
	#################################################################

	[[ -n "$pickfiles" ]] && {
		dir=$(print $dir|
			awk '{s1=index($0,"("); print substr($0,1,s1-1);}');
	}

	cd $PROJECT_PREFIX/$project_name/$dir 2>/dev/null || {
		logwarn "Directory \"$dir\" does not exist - ignoring.";
		continue;
	}

	#################################################################
	# Because of the extended attribute the "DIR:" might be used	#
	# multiple times for the same directory - if so, only repear	#
	# the "directory" line if similar lines are grouped.		#
	#################################################################

	predir=$dir
	predestdir=$destdir

	#################################################################
	# Now dump all the files in this directory...			#
	#################################################################

	for cfile in $(eval ls -d "$pattern" 2>/dev/null)
	do
		[[ ! -f $cfile ]] && continue
		#########################################################
		# Ignore "ignore files...				#
		#########################################################

		myext="$(print $cfile|awk -v FS="." 'NF>1 {print $NF}')"
		[[ "$myext" = "ignore" ]] && continue

		if [[ -f "$cfile.ignore" ]]
		then
			continue
		fi
		
		#################################################
		# Here we check to see if the suffix matches	#
		# one of those in the pkg_ignore_suffixes list.	#
		#################################################

		if [[ -n "$pkg_ignore_suffixes" ]]
		then
			myignore=0
			mysuff=".${cfile##*.}"
			for suffix in $pkg_ignore_suffixes
			do
				if [[ "$suffix" = "$mysuff" ]]
				then
					myignore=1
					break
				fi
			done
			if [[ $myignore -eq 1 ]]
			then
				logmsg "Excluding \"$dir/$cfile\" from package build."
				continue
			fi
		fi
		cp $cfile $LPP_ROOTDIR/$mydir 2>/dev/null ||
			logerr "Unable to copy $cfile to $LPP_ROOTDIR/$mydir" 19

		chmod $perms $LPP_ROOTDIR/$mydir/$cfile 2>/dev/null ||
			logwarn "Unable to set perms to $perms on $LPP_ROOTDIR/$mydir/$cfile"

		if [[ "$myid" = root ]]
		then
			chown $user:$group $LPP_ROOTDIR/$mydir/$cfile 2>/dev/null ||
				logwarn "Unable to set user:group to $user:$group on $LPP_ROOTDIR/$mydir/$cfile"
		fi
	done
	cd $mycd
done

logmsg "Setting package directory permissions..."
for cdir in $setpermlist
do
	print "$cdir" | sed 's/\|/ /g' | read dir user group perms

	#################################################################
	# We define the permissions based on the file permissions,	#
	# treating them as the following binary masks 4=read,2=write,	#
	# 1=execute. 							#
	#								#
	# if write permissions then get rwx to directory.		#
	# if no permissions get --- to directory.			#
	# otherwise get r-x.						#
	#################################################################

	ochar=$(print $perms|awk '{print substr($1,length($1),1)}')
	nchar=$(print $ochar | perl -e '$x=<STDIN>; chomp $x;
			if($x&2) {print "7"; exit;}
			if(!$x) {print "0"; exit;}
			print "5";')

	ochar2=$(print $perms|awk '{print substr($1,length($1)-1,1)}')
	nchar2=$(print $ochar2 | perl -e '$x=<STDIN>; chomp $x;
			if($x&2) {print "7"; exit;}
			if(!$x) {print "0"; exit;}
			print "5";')

	#################################################################
	# We always set the owner permissions to 7 if not root, due to	#
	# the way AIX works with dir perms on rm,cp,mv.			#
	#################################################################
	ochar3=$(print $perms|awk '{print substr($1,length($1)-2,1)}')
	nchar3=$(print $ochar3 | perl -e '$x=<STDIN>; chomp $x;
			if($x&2) {print "7"; exit;}
			if(!$x) {print "0"; exit;}
			print "5";')

	[[ "$myid" != root ]] && nchar3=7

	perms="$nchar3$nchar2$nchar"
	chmod $perms $dir 2>/dev/null ||
		logwarn "Unable to set perms to $perms on $dir"

	if [[ "$myid" = root ]]
	then
		chown $user:$group $dir 2>/dev/null ||
			logwarn "Unable to set user:group to $user:$group on $dir"
	fi
done

mycd=$PWD
((PREVIEW==1)) && {
	logmsg "Preview completed successfully.";
	exit 0;
}

cd $pkg_depot/$pkg_name 2>/dev/null ||
	logerr "Unable to change to directory: $pkg_depot/$pkg_name/allfiles" 21

logmsg "lppbuild is defined as $lppbuild"
logmsg "Building package ..."
$lppbuild >$HOME/lppbuild.out 2>$HOME/lppbuild.err
if [[ $? -eq 0 ]] 
then
	mv $pkg_depot/$pkg_name/$pkg_name.bff $pkg_depot
	logmsg "Package built successfully in depot $pkg_depot/$pkg_name.bff"
	#################################################################
	# If the FINAL_DEPOT has been specified, then we spool the 	#
	# package to a single file and dump in that depot.		#
	#################################################################
	logmsg "$(dirname $pkg_depot/$pkg_name.bff) $(dirname $FINAL_DEPOT/$pkgname)"
	cd $mycd
	rm -rf $pkg_depot/$pkg_name 2>/dev/null
	if [[ -n "$FINAL_DEPOT" ]]
	then
		if [[ "$(dirname $pkg_depot/$pkg_name.bff)" = "$(dirname $FINAL_DEPOT/$pkgname)" ]]
		then
			logmsg "Renaming package to final name..."
			mv -f $pkg_depot/$pkg_name.bff $FINAL_DEPOT/$pkgname 2>$HOME/lppbuild2.err
		else
			print "" > $FINAL_DEPOT/$pkgname
			logmsg "Copying package to final depot..."
			cp $pkg_depot/$pkg_name.bff $FINAL_DEPOT/$pkgname 2>$HOME/lppbuild2.err
		fi
		if [[ $? -eq 0 ]] 
		then
			logmsg "Package copied to $FINAL_DEPOT successfully ($pkgname)"
			exit 0
		else
			logerr "Unable to copy file to $FINAL_DEPOT, error output is:"
			logerrfile $HOME/lppbuild2.err 7
		fi
	fi
	exit 0
else
	logerr "Unable to package application. Error output is:"
	logerrfile $HOME/lppbuild.out
	logerrfile $HOME/lppbuild.err 7
fi

